//
//  NSArray+Sort.m
//  JWBrowser
//
//  Created by Dejun Liu on 15/1/19.
//  Copyright (c) 2015年 JW. All rights reserved.
//

#import "NSArray+Sort.h"

@implementation NSArray (Sort)

- (NSArray *)ldjSortByASC
{
    return [self sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        obj1 = [NSString stringWithFormat:@"%@",obj1];
        obj2 = [NSString stringWithFormat:@"%@",obj2];
        return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
    }];
}

- (NSArray *)ldjSortByDESC
{
    return [self sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        obj1 = [NSString stringWithFormat:@"%@",obj1];
        obj2 = [NSString stringWithFormat:@"%@",obj2];
        NSComparisonResult res = [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
        switch (res) {
            case NSOrderedAscending:
                return NSOrderedDescending;
                break;
            case NSOrderedSame:
                return NSOrderedSame;
                break;
            case NSOrderedDescending:
                return NSOrderedAscending;
                break;
            default:
                break;
        }
    }];
}

- (NSArray *)ldjSortDictionaryOrderWithDictKey:(NSString *)key ASC:(BOOL)isASC
{
    NSSortDescriptor *_sorter  = [[NSSortDescriptor alloc] initWithKey:key
                                                             ascending:isASC];
    return [self sortedArrayUsingDescriptors:@[_sorter]];
}

@end
